﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MikeKappel.Com
{
    
    public static partial class Helper
    {
        public static String[] ImageContentTypes {
            get{
                return new String[]{"image/x-sgi","image/x-photoshop","image/x-bmp","image/vnd.fpx","image/gif","image/jpeg","image/pjpeg","image/pict","image/x-pict","image/png","image/x-png","image/x-macpaint","image/tiff","image/x-tiff","image/x-targa","image/x-quicktime"};
            }   
        }
        public static String[] UploadAllowedFileTypes
        {
            get
            {
                //if (String.IsNullOrEmpty(ConfigurationManager.AppSettings[KeyUploadAllowedFileTypes]))
                //    return null;
                //else if (ConfigurationManager.AppSettings[KeyUploadAllowedFileTypes].Contains(","))
                //    return ConfigurationManager.AppSettings[KeyUploadAllowedFileTypes].Split(',');
                //else
                //    return new String[] { ConfigurationManager.AppSettings[KeyUploadAllowedFileTypes] };
                return null;
            }
        }
        public static Int32 BytesToMegaBytes(Int32 bytes)
        {
            return  bytes * 1048576;
        }
        public static Boolean IsContentTypeImage(String contentType)
        {
            return (Array.IndexOf(ImageContentTypes, contentType) >= 0) ? true : false;
        }
        public static Int32 FirstIndexOf(Byte[] lookin, Byte[] find)
        {
            for (Int32 i = 0; i < lookin.Length; i++)
            {
                if (find[0] == lookin[i] && lookin.Length - i >= find.Length)
                {
                    Boolean ismatch = true;
                    for (Int32 j = 1; j < find.Length && ismatch == true; j++)
                    {
                        if (lookin[i + j] != find[j])
                            ismatch = false;
                    }
                    if (ismatch)
                    {
                        return i;
                    }
                }
            }
            return -1;
        }
        public static String GetUploadContentType(Byte[] upload)
        {
            if (Helper.FirstIndexOf(upload, new Byte[] { 13, 10, 13, 10 }) > 0)
            {
                Int32 contentTypeStartIndex = Helper.FirstIndexOf(upload, (new System.Text.ASCIIEncoding()).GetBytes("Content-Type:".ToCharArray())) + 13;
                Int32 contentTypeEndIndex = Helper.FirstIndexOf(upload, new Byte[] { 13, 10, 13, 10 });
                Byte[] contentTypeHeaderBytes = new Byte[contentTypeEndIndex - contentTypeStartIndex];
                Array.Copy(upload, contentTypeStartIndex, contentTypeHeaderBytes, 0, contentTypeEndIndex - contentTypeStartIndex);
                return ASCIIEncoding.ASCII.GetString(contentTypeHeaderBytes).Trim();
            }
            else
            {
                return null;
            }
        }

    }
}
